package org.yanex.project.api;

public class APIFactory {

	private static volatile APIFactory instance;

	public static enum Protocol {
		XML, JSON
	}

	public static class UnsupportedProtocolException extends Exception {
		private static final long serialVersionUID = -50618606213101062L;
		
	}

	public static APIFactory getInstance() {
		APIFactory localInstance = instance;
		if (localInstance == null) {
			synchronized (APIFactory.class) {
				localInstance = instance;
				if (localInstance == null) {
					instance = localInstance = new APIFactory();
				}
			}
		}
		return localInstance;
	}

	/**
	 * @param server Server URL (eg. http://localhost:80/api/");
	 * @param protocol Protocol type.
	 * @return Ready-for-work API object.
	 * @throws UnsupportedProtocolException When the specified protocol
	 * 		is not supported.
	 */
	public API createAPI(String server, Protocol protocol)
			throws UnsupportedProtocolException {
		switch (protocol) {
		case XML:
			return new APIXMLImpl(server);
		case JSON:
			throw new UnsupportedProtocolException();
		default:
			throw new UnsupportedProtocolException();
		}
	}

}
